package hu.bme.aut.consulttime.client.components;

import java.util.List;

import hu.bme.aut.consulttime.client.ProjectService;
import hu.bme.aut.consulttime.client.ProjectServiceAsync;
import hu.bme.aut.consulttime.entitydtos.ProjectDTO;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ProjectInfoPage extends Composite {
	
	private long projectId;
	
	private VerticalPanel mainPanel;
	private Label lblProjectTitle;
	private Label lblProjectType;
	private Label lblProjectDescription;
	private FlexTable projectTutorsFlexTable;
	
	private ProjectServiceAsync projectService = GWT.create(ProjectService.class);
	
	public ProjectInfoPage() {
		super();
		
		mainPanel = new VerticalPanel();
		initWidget(mainPanel);
		mainPanel.setSize("100%", "100%");
		
		VerticalPanel verticalPanel_2 = new VerticalPanel();
		mainPanel.add(verticalPanel_2);
		verticalPanel_2.setSize("100%", "");
		verticalPanel_2.setSpacing(5);
		
		HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_2);
		horizontalPanel_2.setWidth("100%");
		
		Label lblNewLabel_3 = new Label("Title:");
		horizontalPanel_2.add(lblNewLabel_3);
		lblNewLabel_3.setWidth("80px");
		
		lblProjectTitle = new Label("");
		horizontalPanel_2.add(lblProjectTitle);
		lblProjectTitle.setWidth("100%");
		horizontalPanel_2.setCellWidth(lblProjectTitle, "100%");
		
		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_1);
		horizontalPanel_1.setWidth("100%");
		
		Label lblNewLabel_1 = new Label("Type:");
		horizontalPanel_1.add(lblNewLabel_1);
		lblNewLabel_1.setWidth("80px");
		
		lblProjectType = new Label("");
		horizontalPanel_1.add(lblProjectType);
		lblProjectType.setWidth("100%");
		horizontalPanel_1.setCellWidth(lblProjectType, "100%");
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel);
		horizontalPanel.setWidth("100%");
		
		Label lblDescription = new Label("Description:");
		horizontalPanel.add(lblDescription);
		lblDescription.setWidth("80px");
		
		lblProjectDescription = new Label();
		horizontalPanel.add(lblProjectDescription);
		horizontalPanel.setCellWidth(lblProjectDescription, "100%");
		lblProjectDescription.setWidth("100%");
		lblProjectDescription.getElement().getStyle().setProperty("whiteSpace", "pre");
		lblProjectDescription.setWordWrap(true);
		
		HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_3);
		
		Label lblTeamMembers = new Label("Tutors:");
		horizontalPanel_3.add(lblTeamMembers);
		
		projectTutorsFlexTable = new FlexTable();
		mainPanel.add(projectTutorsFlexTable);
		mainPanel.setCellHorizontalAlignment(projectTutorsFlexTable, HasHorizontalAlignment.ALIGN_RIGHT);
		projectTutorsFlexTable.setBorderWidth(1);
		projectTutorsFlexTable.setSize("100%", "100%");
		projectTutorsFlexTable.setText(0, 0, "Full name");
		projectTutorsFlexTable.setText(0, 1, "Username");
		projectTutorsFlexTable.setText(0, 2, "E-mail");
	}
	

	public long getProjectId() {
		return projectId;
	}

	public void setProjectId(long projectId) {
		this.projectId = projectId;
	}

	public void loadProject() {
		projectService.retrieveProject(projectId, new AsyncCallback<ProjectDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");
			}

			@Override
			public void onSuccess(ProjectDTO result) {
				lblProjectTitle.setText(result.getTitle());
				lblProjectType.setText(result.getType().toString());
				lblProjectDescription.setText(result.getDescription());
			}
			
		});
		
	}
	
	public void loadProjectTutors() {
		projectTutorsFlexTable.removeAllRows();
		projectTutorsFlexTable.setText(0, 0, "Full name");
		projectTutorsFlexTable.setText(0, 1, "Username");
		projectTutorsFlexTable.setText(0, 2, "E-mail");
		
		projectService.retrieveTutors(projectId, new AsyncCallback<List<UserDTO>>() {
			
			@Override
			public void onSuccess(List<UserDTO> result) {
				int rownum = 1;
				for (UserDTO userDTO : result) {
					projectTutorsFlexTable.setText(rownum, 0, userDTO.getFirstName() + " " + userDTO.getLastName());
					projectTutorsFlexTable.setText(rownum, 1, userDTO.getUserName());
					projectTutorsFlexTable.setText(rownum, 2, userDTO.getEmail());
					rownum++;
				}
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");				
			}
		});
		
	}
}
